#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <time.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <stdarg.h>
#include <arpa/inet.h>
#include "ftpUpload.h"
#include "xml_conf.h"
#include "./../3rdparty/include/openssl/des.h"
#include "./../flexapi/ftpclient/cftp.h"
const char *recordDir="/mnt/usr/upload";
static bool uploadSuccess=true;
FTPDIR ftpDir;
FILE *fd= NULL;
static I8 mac17[20] = {0};
static I8 mac12[16] = {0};
I8 fileName[DEV_INFO_PAR2_LEN]={0};
I8 ftpFile[DEV_INFO_PAR2_LEN] = {0};
I8 ftpFileDir[DEV_INFO_PAR_LEN] = {0};
static bool ftpuploadSuccess = false;
static bool ftpUploadOkSucc = false;
static bool ftpCreateDir = false;
static KAUNGAUNG_FTP kuanGuangFtp;
bool createFtpDir(U8 type);
const char *carrierCode[12] =
{
    "20",//gsm mobile
    "22", //gsm unicon
    "23", //tdscdma
    "32",//wcdma
    "33",//cdma
    "40",//tdd_lte D
    "41",//tdd_lte_E
    "42",//tdd_lte_F
    "44",//fdd_lte_unicon
    "45",//fdd_lte_unicon
    "47",//fdd_lte_tel
    "48"//fdd_lte_tel
};

const char *WXcarrierCode[8] =
{
	"20",	//gsm mobile
	"22",	//gsm unicom
	"30",	//td-scdma
	"32",	//wcdma
	"40",	//lte_D
	"42",	//td_lte
	"43",	//fdd_unicom
	"44"	//fdd_tel
};

U8 OperatorCode[4] = 
{
	0,	//	mobile
	1,	//	unicom
	2, 	//	telecom
	4	//	unknown
};

U8 NetworkCode[4] = 
{
	0,	//unknown
	1,	// 2g
	2,	// 3g
	3	// 4g
};

bool getUploadState()
{
    return uploadSuccess;
}

void ftpInit()
{
    DIR *pDir = NULL;
    pDir = opendir(recordDir);
    if (pDir == NULL) 
    {
        if (mkdir(recordDir, 0777) != 0)
        {
            cli_app_log(CLI_LOG_ERROR, "create upload dir %s falied\n", recordDir);
            return;
        }
    }
    else
        closedir(pDir);
    system("rm /mnt/usr/upload/* -rf");
    memset(&kuanGuangFtp, 0x0, sizeof(KAUNGAUNG_FTP));
    memset(&ftpDir, 0x0, sizeof(FTPDIR));
    getmac(mac17, mac12);

}

I32 renameLoaclFtpFile()
{
    char remoteName[DEV_INFO_PAR2_LEN] = {0};
    char newName[DEV_INFO_PAR2_LEN] = {0};
    if (strcmp(dev_conf.dev.protocol, FENGHUO) == 0)
    {
        if (kuanGuangFtp.uploadType != 3)
        {
            sprintf(remoteName, "%s.tmp", ftpFile);
            sprintf(newName, "%s.zip", ftpFile);
        }
        else
        {
            sprintf(remoteName, "%s.tmp", ftpFile);
            sprintf(newName, "%s.bjson", ftpFile);

        }
    }
    else if (strcmp(dev_conf.dev.protocol, XINAN) == 0)
    {
        sprintf(remoteName, "%s.tmp", ftpFile);
        sprintf(newName, "%s.txt", ftpFile);

    }
    else if (strcmp(dev_conf.dev.protocol, RENZIXING) == 0)
    {
        sprintf(remoteName, "%s%s.tmp", ftpFileDir, ftpFile);
        sprintf(newName, "%s%s", ftpFileDir, ftpFile);

    }
    return renameFtpFile(dev_conf.ftp.ftpserver, atoi(dev_conf.ftp.ftpport), dev_conf.ftp.ftpuser, 
                        dev_conf.ftp.ftppasswd, remoteName, newName);
}

bool createZipFile()
{
    I8 buf[2048] = {0};
    sprintf(buf, "zip -p -j /mnt/usr/upload/%s  /mnt/usr/upload/* \n", "tmp.zip");
    pid_t status;
    status = system(buf);
    if (status == -1)
    {
        cli_app_log(CLI_LOG_ERROR, "cmd filed %s  status %d\n", buf, status);
        return false;
    }
    else
    {
        if ( WIFEXITED(status) )
        {
            cli_app_log(CLI_LOG_DEFAULT, "cmd exit normal \n");
            if ( WEXITSTATUS(status) == 0)
            {
                cli_app_log(CLI_LOG_DEFAULT, "create zip file success \n");
            }   
        }
        else
        {
            cli_app_log(CLI_LOG_NOTICE, "cmd exit abnormal\n");
        }
    }
    struct stat fileStat;
    I32 tmp = 0;
    tmp = stat("/mnt/usr/upload/tmp.zip", &fileStat);
    if (tmp == -1)
    {
        cli_app_log(CLI_LOG_WARNING, "no such zip file \n");
        return false;
    }
    FILE *zipFd = fopen("/mnt/usr/upload/tmp.zip", "rb");
    if (zipFd == NULL)
        return false;
    time_t uptime;
    time(&uptime);
    I32 fileNum = atoi(dev_conf.ftp.ftpCompressFileNum);
    sprintf(fileName, "%s/%s20-145-%s-%s-%s-%lu-%05d.zip", recordDir, dev_conf.ftp.ftpbsic, dev_conf.ftp.ftpcompany,
            dev_conf.ftp.ftpDataSource, dev_conf.ftp.ftpDataDest, uptime, fileNum++);
    if (fileNum > 99999)
        fileNum = 0;
    memset(dev_conf.ftp.ftpCompressFileNum, 0x0, DEV_INFO_NUM_LEN);
    sprintf(dev_conf.ftp.ftpCompressFileNum, "%d", fileNum);
    FILE *file = fopen(fileName, "a+");
    if (file == NULL)
    {
        return false;    
    }
    unsigned char *fbuf = (unsigned char *)malloc(fileStat.st_size + 8);
    if (fbuf == NULL)
        return false;
    fread(fbuf, 1, fileStat.st_size, zipFd);
    fclose(zipFd);
    I32 len = htonl(fileStat.st_size);
    memcpy((fbuf + fileStat.st_size), &len, 4);
    memcpy((fbuf + fileStat.st_size + 4), "ABCD", 4);
    int i = 0;
    unsigned char perdata[64] = {0};
    unsigned char encrydata[64] = {0};
    DES_cblock ivec;
    memcpy(&ivec, dev_conf.ftp.ftpIV, strlen(dev_conf.ftp.ftpIV));
    DES_key_schedule ks;
    DES_set_key_unchecked((const_DES_cblock *)dev_conf.ftp.ftpKey, &ks);
    cli_app_log(CLI_LOG_DEFAULT, "file name is %s size is %d key %s iv %s\n", fileName, fileStat.st_size + 8, dev_conf.ftp.ftpKey, dev_conf.ftp.ftpIV);
    for (; i < (fileStat.st_size + 8); ) 
    {
        memset(perdata, '\0', 64);
        if ( i < (fileStat.st_size + 8))
        {
            if (i + 64 > (fileStat.st_size + 8))
                memcpy(perdata, fbuf + i, (fileStat.st_size + 8 - i));
            else
                memcpy(perdata, fbuf+i, 64);
            DES_ncbc_encrypt(perdata, encrydata, 64, &ks, &ivec, DES_ENCRYPT);  
            fwrite(encrydata, 64, 1, file);
        }
        i += 64;
    }
    free(fbuf);
    fclose(file);
    struct stat fileStat1;
    stat(fileName, &fileStat1);
    cli_app_log(CLI_LOG_INFO, "file size is %d \n", fileStat1.st_size);

    return true;
}


bool uploadFile()
{
    if (fd != NULL)
    {
        if (strcmp(dev_conf.dev.protocol, RENZIXING) == 0 || strcmp(dev_conf.dev.protocol, GAOKE) == 0)
        {
            fprintf(fd, "%s", "]");
            fflush(fd);
        }

        fclose(fd);
        fd = NULL;
    }
    uploadSuccess= false;
    if (strcmp(dev_conf.dev.protocol, KUANGUANG) == 0)
    {
        if (kuanGuangFtp.fileNum > 0)
        {
            if (CreateKuanGuangXmlFile(&kuanGuangFtp) != IF_APP_OK)
            {
                return false;
            }

            if (!createZipFile())
                return false;
            kuanGuangFtp.fileNum = 0;
            
        }


    }
    else if(strcmp(dev_conf.dev.protocol, FENGHUO) == 0)
	{
		
		if(kuanGuangFtp.fileNum > 0)
		{
			if (kuanGuangFtp.uploadType == 1)
            {

                if(CreateFengHuoDataPlaceXmlFile(&kuanGuangFtp)!= IF_APP_OK)	
                {
                    return false;
                }	
            }
            else if (kuanGuangFtp.uploadType == 2)
            {

                if(CreateFengHuoDataFirmXmlFile(&kuanGuangFtp)!= IF_APP_OK)	
                {
                    return false;
                }	
            }
            else if(kuanGuangFtp.uploadType == 4)
			{
			
				if(CreateFengHuoDataTerminalXmlFile(&kuanGuangFtp)!= IF_APP_OK) 
				{
					return false;
				}	
			}
			else if(kuanGuangFtp.uploadType == 5)
			{
			
				if(CreateFengHuoDataMacXmlFile(&kuanGuangFtp)!= IF_APP_OK) 
				{
					return false;
				}	
			}
            time_t uptime;
            time(&uptime);
            I32 fileNum = atoi(dev_conf.ftp.ftpCompressFileNum);
			if(kuanGuangFtp.uploadType != 0)
            	sprintf(ftpFile, "146-%s-%s-%s-%lu-%05d", dev_conf.ftp.ftpcompany, dev_conf.ftp.ftpDataSource, dev_conf.ftp.ftpDataDest, uptime, fileNum++);
            cli_app_log(CLI_LOG_INFO, "%s \n", ftpFile);
            if (fileNum > 99999)
                fileNum = 0;
            memset(dev_conf.ftp.ftpCompressFileNum, 0x0, DEV_INFO_NUM_LEN);
            sprintf(dev_conf.ftp.ftpCompressFileNum, "%d", fileNum);
			kuanGuangFtp.fileNum = 0;
            if (kuanGuangFtp.uploadType != 3)
            {
				if(kuanGuangFtp.uploadType != 0)
				{
					I8 tmpBuf[1024] = {0};
	                sprintf(tmpBuf, "zip -p -j /mnt/usr/upload/%s.zip  /mnt/usr/upload/* \n", ftpFile);
	                pid_t status;
	                status = system(tmpBuf);
	                if (status == -1)
	                {
	                    cli_app_log(CLI_LOG_ERROR, "cmd filed %s  status %d\n", tmpBuf, status);
	                    return false;
	                }
	                memset(fileName, 0, DEV_INFO_PAR2_LEN);
	                sprintf(fileName, "%s/%s.zip", recordDir, ftpFile);
	                cli_app_log(CLI_LOG_INFO, "zip filename is %s \n", fileName);
				}
				else
					sprintf(fileName, "%s/%s.bjson", recordDir, ftpFile);
            }

		}

	}
    else if(strcmp(dev_conf.dev.protocol, WANGXUN) == 0)
	{
        I8 tmpBuf[1024] = {0};
		sprintf(tmpBuf, "zip -p -j  %s.zip  %s \n", ftpFile, fileName);
	    pid_t status;
	    status = system(tmpBuf);
	    if (status == -1)
	    {
	        cli_app_log(CLI_LOG_ERROR, "cmd filed %s  status %d\n", tmpBuf, status);
	        return false;
	    }
        memset(fileName, 0, DEV_INFO_PAR2_LEN);
		sprintf(fileName, "%s.zip", ftpFile);
		cli_app_log(CLI_LOG_INFO, "zip filename is %s \n", fileName)
	}
    else if (strcmp(dev_conf.dev.protocol, RENZIXING) == 0)
    {
        /*
        if (kuanGuangFtp.fileNum  > 0)
        {
            if (kuanGuangFtp.uploadType == IMSI_DATA_TYPE && (strstr(fileName ,"ok") == NULL))
            {
                I8 tmpBuf[1024] = {0};
                I8 szbuf[64] = {0};
                time_t uptime;
                time(&uptime);
                memset(szbuf, 0, 64);
                strftime(szbuf, 64, "%Y%m%d%H%M%S", localtime(&uptime));
                sprintf(ftpFile, "%s%03d_146_%s_%s_014.zip", szbuf, rand()%1000, dev_conf.ftp.ftpDataSource, 
                        dev_conf.ftp.ftpcompany);
                sprintf(tmpBuf, "zip -p -j  /mnt/usr/upload/%s.tmp  %s \n", ftpFile, fileName);
                pid_t status;
                status = system(tmpBuf);
                if (status == -1)
                {
                    cli_app_log(CLI_LOG_ERROR, "cmd filed %s  status %d\n", tmpBuf, status);
                    return false;
                }
                memset(fileName, 0, DEV_INFO_PAR2_LEN);
                sprintf(fileName, "%s/%s.tmp", recordDir, ftpFile);
                cli_app_log(CLI_LOG_INFO, "zip filename is %s \n", fileName)
            }
        }*/
    }

    I8 usrAndpwd[DEV_INFO_PAR2_LEN] = {0};
    I8 ftpAddr[DEV_INFO_ADD_LEN]={0};
    if (strcmp(dev_conf.dev.protocol, RENZIXING) == 0)
    {
        memset(ftpFileDir, 0, DEV_INFO_PAR_LEN);
        if ((strstr(fileName ,"ok") == NULL))
        {
            if (!createFtpDir(kuanGuangFtp.uploadType))
            {
                ftpCreateDir = false;
                cli_app_log(CLI_LOG_NOTICE, "create ftp dir failed\n"); 
                return true;
            }
        }
        ftpCreateDir = true;
        sprintf(ftpFileDir, "/%s/%s/%s/%s/", ftpDir.typeName, ftpDir.year, ftpDir.hour, ftpDir.min);
        sprintf(ftpAddr, "ftp://%s:%s%s", dev_conf.ftp.ftpserver, dev_conf.ftp.ftpport, ftpFileDir);
        
        cli_app_log(CLI_LOG_INFO, "addr is %s %d\n", ftpAddr, kuanGuangFtp.uploadType);
    }
    else if (strcmp(dev_conf.dev.protocol, GAOKE) == 0)
    {
        sprintf(ftpAddr, "ftp://%s:%s%s", dev_conf.ftp.ftpserver, dev_conf.ftp.ftpport, dev_conf.ftp.ftpdir);
    }
    else
    {
        sprintf(ftpAddr, "ftp://%s:%s", dev_conf.ftp.ftpserver, dev_conf.ftp.ftpport);
    }
    sprintf(usrAndpwd, "%s:%s", dev_conf.ftp.ftpuser, dev_conf.ftp.ftppasswd);
    pid_t fpid = fork();

    I32 ret = 0;
    if (fpid == 0)
    {
        ret = execlp("curl", "curl", "-u", usrAndpwd, "-T", fileName, ftpAddr, NULL);   
        if (ret == -1)
        {
            cli_app_log(CLI_LOG_ERROR, "execlp curl fiald error:%s \n", strerror(errno));
            return false;
        }
    }
    else if (fpid < 0)
    {
        cli_app_log(CLI_LOG_ERROR, "ftp fork failed");
        return false;
    }
    else
    {
        if (ret == -1)
            return false;
        cli_app_log(CLI_LOG_INFO, "curl pid is %d \n", fpid);
    
    }
    return true;
}

bool uploadLogFile()
{
    I8 logFile[DEV_INFO_PAR_LEN] = {0};
    const char *cmd="ls -l /mnt/usr/alll*.tar.gz |awk '{print $9}'";
    FILE *fpipe = (FILE *)popen(cmd, "r");
    if (fpipe == NULL)
    {
        return false;
    }
    if( NULL == fgets(logFile, DEV_INFO_PAR_LEN, fpipe))
    {
        if( 0 == errno )
        {
            pclose(fpipe);
            return true;
        }
        pclose(fpipe);
        return false;
    }
    pclose(fpipe);
    logFile[strlen(logFile) - 1] = '\0';
    cli_app_log(CLI_LOG_NOTICE, "log file is %s len is %d \n", logFile, strlen(logFile));
    if (access(logFile, F_OK) == 0)
    {
        pid_t fpid = fork();
        I8 usrAndpwd[DEV_INFO_PAR2_LEN] = {0};
        I8 ftpAddr[DEV_INFO_ADD_LEN]={0};
        sprintf(ftpAddr, "ftp://%s:%d", ftpLog.server, ftpLog.port);
        sprintf(usrAndpwd, "%s:%s", ftpLog.user, ftpLog.passwd);
        I32 ret = 0;
        if (fpid == 0)
        {
            //ret = execlp("curl", "curl", "-u", usrAndpwd, "-T", logFile, ftpAddr, "--no-epsv", NULL);
				cli_app_log(CLI_LOG_INFO, "exec curl shell \n");
				execlp("/usr/bin/test_curl.sh", "/usr/bin/test_curl.sh", usrAndpwd, logFile, ftpAddr, NULL);

            if (ret == -1)
            {
                cli_app_log(CLI_LOG_ERROR, "execlp curl fiald error:%s \n", strerror(errno));
                return false;
            }
        }
        else if (fpid < 0)
        {
            cli_app_log(CLI_LOG_ERROR, "ftp fork failed");
            return false;
        }
        
    }
    else
    {
        cli_app_log(CLI_LOG_WARNING, "can not find file %s \n", logFile);
    }
    return true;
}

I32 checkUpLog()
{
    I32 rc = 0;
    pid_t pid = waitpid(-1, &rc, WNOHANG);
    if (pid < 0)
    {
        I32 err = errno;
        if (err == ECHILD)
        {
            cli_app_log(CLI_LOG_ERROR, "No child process \n");
            return PID_ERROR;
        }
        else if (err == EINTR)
        {
        
        }
        else
        {
            cli_app_log(CLI_LOG_ERROR, "fail to waitpid , errno=%d n", errno);
        }
    }
    if (pid > 0)
    {
        if (rc == 0)
        {
            return PID_SUCCESS;
        }
        else
        {
            cli_app_log(CLI_LOG_NOTICE, "failed to upload file status is %d \n",rc);
            return PID_ERROR;
        }
    }
    else
    {
        cli_app_log(CLI_LOG_DEFAULT, "upload file is on going, error=%d \n", errno);
    }
    return PID_GOING;

}

I32 checkUpload()
{
    if ((strcmp(dev_conf.dev.protocol, XINAN) == 0) && ftpuploadSuccess)
    {
        I32 ret = 0;
        ret = renameLoaclFtpFile();
        if (ret == FTP_FAILED)
            return PID_GOING;
        else if (ret == FTP_FILE_ERROR)
        {
            ftpuploadSuccess = false;
            return PID_ERROR;
        }
        else
        {
            uploadSuccess = true;
            ftpuploadSuccess = false;
            cli_app_log(CLI_LOG_INFO, "upload %s success child process\n", fileName);
            system("rm /mnt/usr/upload/*");
            memset(&kuanGuangFtp, 0x0, sizeof(KAUNGAUNG_FTP));
            return PID_SUCCESS;

        }
    }
    
    if (strcmp(dev_conf.dev.protocol, RENZIXING) == 0)
    {
        if (!ftpCreateDir)
        {
            cli_app_log(CLI_LOG_INFO, "create ftp dir again\n");
            return PID_ERROR;
        }
        if (ftpuploadSuccess && !ftpUploadOkSucc && (strstr(ftpFile ,"ok") == NULL))
        {
            I32 ret = 0;
            ret = renameLoaclFtpFile();
            if (ret == FTP_FAILED)
                return PID_GOING;
            else if (ret == FTP_FILE_ERROR)
            {
                ftpuploadSuccess = false;
                return PID_ERROR;
            }
            else
            {
                cli_app_log(CLI_LOG_INFO, "upload %s success child process\n", fileName);
                memset(fileName, 0, DEV_INFO_PAR2_LEN);
                strcat(ftpFile, ".ok");
                sprintf(fileName, "%s/%s.tmp", recordDir, ftpFile);
                I8 tmpBuf[512] = {0};
                sprintf(tmpBuf, "touch %s", fileName);
                system(tmpBuf);
                ftpUploadOkSucc = false;;
                return PID_ERROR;
            }
        }
        else if (ftpuploadSuccess && ftpUploadOkSucc)
        {

            I32 ret = 0;
            ret = renameLoaclFtpFile();
            if (ret == FTP_FAILED)
                return PID_GOING;
            else if (ret == FTP_FILE_ERROR)
            {
                ftpUploadOkSucc = false;
                return PID_ERROR;
            }
            else
            {
                uploadSuccess = true;
                ftpuploadSuccess = false;
                ftpUploadOkSucc = false;
                cli_app_log(CLI_LOG_INFO, "upload %s success \n", fileName);
                system("rm /mnt/usr/upload/*");
                memset(&kuanGuangFtp, 0x0, sizeof(KAUNGAUNG_FTP));
                return PID_SUCCESS;
            }


        }

    }


    I32 rc = 0;
    pid_t pid = waitpid(-1, &rc, WNOHANG);
    if (pid < 0)
    {
        I32 err = errno;
        if (err == ECHILD)
        {
            cli_app_log(CLI_LOG_ERROR, "No child process \n");
            return PID_ERROR;
        }
        else if (err == EINTR)
        {
        
        }
        else
        {
            cli_app_log(CLI_LOG_ERROR, "fail to waitpid , errno=%d n", errno);
        }
    }
    if (pid > 0)
    {
        if (rc == 0)
        {
    
            if (strcmp(dev_conf.dev.protocol, XINAN) != 0 && 
                 strcmp(dev_conf.dev.protocol, RENZIXING) != 0)
            {
                uploadSuccess = true;
                cli_app_log(CLI_LOG_INFO, "upload %s success child process[%d]\n", fileName, pid);
                I8 cmd[DEV_INFO_PAR2_LEN] = {0};
                system("rm /mnt/usr/upload/*");
                memset(&kuanGuangFtp, 0x0, sizeof(KAUNGAUNG_FTP));
                return PID_SUCCESS;
            }
            else
            {
                if (strcmp(dev_conf.dev.protocol, RENZIXING) != 0)
                {
                    ftpuploadSuccess = true;
                    I32 ret = 0;
                    ret = renameLoaclFtpFile();
                    if (ret == FTP_FAILED)
                        return PID_GOING;
                    else if (ret == FTP_FILE_ERROR)
                    {
                        ftpuploadSuccess = false;
                        return PID_ERROR;
                    }
                    else
                    {
                        uploadSuccess = true;
                        ftpuploadSuccess = false;
                        cli_app_log(CLI_LOG_INFO, "upload %s success child process[%d]\n", fileName, pid);
                        system("rm /mnt/usr/upload/*");
                        memset(&kuanGuangFtp, 0x0, sizeof(KAUNGAUNG_FTP));
                        return PID_SUCCESS;

                    }
                }
                else
                {
                    if (!ftpuploadSuccess && !ftpUploadOkSucc)
                    {
                        ftpuploadSuccess = true;
                        I32 ret = 0;
                        ret = renameLoaclFtpFile();
                        if (ret == FTP_FAILED)
                            return PID_GOING;
                        else if (ret == FTP_FILE_ERROR)
                        {
                            ftpuploadSuccess = false;
                            return PID_ERROR;
                        }
                        ftpuploadSuccess = true;
                        memset(fileName, 0, DEV_INFO_PAR2_LEN);
                        strcat(ftpFile, ".ok");
                        sprintf(fileName, "%s/%s.tmp", recordDir, ftpFile);
                        I8 tmpBuf[512] = {0};
                        sprintf(tmpBuf, "touch %s", fileName);
                        cli_app_log(CLI_LOG_INFO, "touch file %s \n", fileName);
                        system(tmpBuf);
                        ftpUploadOkSucc = false;;
                        return PID_ERROR;
                    }
                    else if ( ftpuploadSuccess && !ftpUploadOkSucc)
                    {
                        ftpUploadOkSucc = true;
                        cli_app_log(CLI_LOG_INFO, "upload ok file %s \n", fileName);
                        I32 ret = 0;
                        ret = renameLoaclFtpFile();
                        if (ret == FTP_FAILED)
                            return PID_GOING;
                        else if (ret == FTP_FILE_ERROR)
                        {
                            ftpUploadOkSucc = false;
                            return PID_ERROR;
                        }
                        else
                        {
                            uploadSuccess = true;
                            ftpuploadSuccess = false;
                            ftpUploadOkSucc = false;
                            cli_app_log(CLI_LOG_INFO, "upload %s success child process[%d]\n", fileName, pid);
                            system("rm /mnt/usr/upload/*");
                            memset(&kuanGuangFtp, 0x0, sizeof(KAUNGAUNG_FTP));
                            return PID_SUCCESS;


                        }
                    }
                    else
                    {   
                        uploadSuccess = true;
                        ftpuploadSuccess = false;
                        ftpUploadOkSucc = false;
                        cli_app_log(CLI_LOG_INFO, "upload %s success child process[%d]\n", fileName, pid);
                        system("rm /mnt/usr/upload/*");
                        memset(&kuanGuangFtp, 0x0, sizeof(KAUNGAUNG_FTP));
                        return PID_SUCCESS;

                    }
                }

            }
        }
        else
        {
            cli_app_log(CLI_LOG_NOTICE, "failed to upload file status is %d \n",rc);
            if (access(fileName, F_OK) != 0)
            {
                cli_app_log(CLI_LOG_ERROR, "can not find file %s \n", fileName);
                uploadSuccess = true;
                return PID_SUCCESS;
            }
            return PID_ERROR;
        }
    }
    else
    {
        cli_app_log(CLI_LOG_DEFAULT, "upload file is on going, error=%d \n", errno);
    }
    return PID_GOING;
}
const char *getCarrierCode(I32 slotId)
{
    I32 ret = 5;
    switch(slotId)
    {
        case 8:
            ret = 0;
            break;
        case 9:
            ret = 1;
            break;
        case 12:
            ret = 2;
            break;
        case 11:
            ret = 3;
            break;
        case 10:
            ret = 4;
            break;
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
        case 6:
        case 7:
            {
                if (atoi(dev_conf.capLte.lte[slotId - 1].band) == 38)
                    ret = 5;
                else if (atoi(dev_conf.capLte.lte[slotId - 1].band) == 40)
                    ret = 6;
                else if (atoi(dev_conf.capLte.lte[slotId - 1].band) == 39)
                    ret = 7;
                else
                {
                    if (strstr(dev_conf.capLte.lte[slotId -1].plmnid, "46011") != NULL )
                        ret = 10;
                    else if (strstr(dev_conf.capLte.lte[slotId -1].plmnid, "46001") != NULL )
                        ret = 8;

                }
                break;
            }
        default:
            break;

    }
    return carrierCode[ret];

}

const char *WXgetCarrierCode(I32 slotId)
{
    I32 ret = 5;
    switch(slotId)
    {
        case 8:
            ret = 0;
            break;
        case 9:
            ret = 1;
            break;
        case 12:
            ret = 2;
            break;
        case 11:
            ret = 3;
            break;
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
        case 6:
        case 7:
            {
                if (atoi(dev_conf.capLte.lte[slotId - 1].band) == 38)
                    ret = 4;
                else if (atoi(dev_conf.capLte.lte[slotId - 1].band) == 40 || atoi(dev_conf.capLte.lte[slotId - 1].band) == 39)
                    ret = 5;
                else
                {
                    if (strstr(dev_conf.capLte.lte[slotId -1].plmnid, "46011") != NULL )
                        ret = 7;
                    else if (strstr(dev_conf.capLte.lte[slotId -1].plmnid, "46001") != NULL )
                        ret = 6;

                }
                break;
            }
        default:
            break;

    }
    return WXcarrierCode[ret];

}

const U8 JFgetOperatorCode(I32 slotId)
{
    U8 ret = 1;
    switch(slotId)
    {
        case 8:
            ret = 1;
            break;
        case 9:
            ret = 2;
            break;
        case 12:
            ret = 2;
            break;
        case 11:
            ret = 3;
            break;
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
        case 6:
        case 7:
            {
                if (atoi(dev_conf.capLte.lte[slotId - 1].band) == 38)
                    ret = 1;
                else if (atoi(dev_conf.capLte.lte[slotId - 1].band) == 40 || atoi(dev_conf.capLte.lte[slotId - 1].band) == 39)
                    ret = 1;
                else
                {
                    if (strstr(dev_conf.capLte.lte[slotId -1].plmnid, "46011") != NULL )
                        ret = 3;
                    else if (strstr(dev_conf.capLte.lte[slotId -1].plmnid, "46001") != NULL )
                        ret = 2;

                }
                break;
            }
        default:
            break;

    }
    return ret;
}

const U8 JFgetCarrierCode(I32 slotId)
{
    U8 ret = 1;
    switch(slotId)
    {
        case 8:
            ret = 1;
            break;
        case 9:
            ret = 1;
            break;
        case 12:
            ret = 2;
            break;
        case 11:
            ret = 4;
            break;
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
        case 6:
        case 7:
            {
                if (atoi(dev_conf.capLte.lte[slotId - 1].band) == 38)
                    ret = 1;
                else if (atoi(dev_conf.capLte.lte[slotId - 1].band) == 40 || atoi(dev_conf.capLte.lte[slotId - 1].band) == 39)
                    ret = 1;
                else
                {
                    if (strstr(dev_conf.capLte.lte[slotId -1].plmnid, "46011") != NULL )
                        ret = 3;
                    else if (strstr(dev_conf.capLte.lte[slotId -1].plmnid, "46001") != NULL )
                        ret = 2;

                }
                break;
            }
        default:
            break;

    }
    return ret;

}

const char *GKgetOperatorCode(I32 slotId)
{
    switch(slotId)
    {
        case 8:
            return "中国移动";
            break;
        case 9:
            return "中国联通";
            break;
        case 12:
            return "中国联通";
            break;
        case 11:
            return "中国电信";
            break;
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
        case 6:
        case 7:
            {
                if (atoi(dev_conf.capLte.lte[slotId - 1].band) == 38)
                    return "中国移动";
                else if (atoi(dev_conf.capLte.lte[slotId - 1].band) == 40 || atoi(dev_conf.capLte.lte[slotId - 1].band) == 39)
                    return "中国移动";
                else
                {
                    if (strstr(dev_conf.capLte.lte[slotId -1].plmnid, "46011") != NULL )
                        return "中国电信";
                    else if (strstr(dev_conf.capLte.lte[slotId -1].plmnid, "46001") != NULL )
                        return "中国联通";

                }
                break;
            }
        default:
            break;

    }
    return "中国移动";
}

U8 ZSgetOperatorCode(U8 *data)
{
	U8 ret = 0;
	const char * pdata = (const char *)data;
	if(strstr(pdata, "46000") != NULL || strstr(pdata, "46002") != NULL || strstr(pdata, "46004") != NULL || strstr(pdata, "46007") != NULL)
	{
		ret = 0;
	}
	else if(strstr(pdata, "46001") != NULL || strstr(pdata, "46006") != NULL || strstr(pdata, "46009") != NULL)
	{
		ret = 1;
	}
	else if(strstr(pdata, "46003") != NULL || strstr(pdata, "46005") != NULL || strstr(pdata, "46011") != NULL)
	{
		ret = 2;
	}
	else 
	{
		ret = 3;
	}
	return OperatorCode[ret];

}

U8 ZSgetNetworkCode(I32 slotId)
{
	U8 ret = 0;
	switch(slotId)
	{
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
        case 6:
        case 7:
            ret = 3;
            break;
        case 8:
        case 9:
        	ret = 1;
        	break;
        case 10:
        case 11:
        case 12:
        	ret = 2;
        	break;
        default:
        	break;
		
	}

	return NetworkCode[ret];

}

bool saveFtpdata(UPDATA_CACHE *updata, PHONE_TMP *phone, I32 *len)
{
    PHONE_DATA *pdata = NULL;
    I32 num = phone->num , i = 0;
    if (num == 0 && updata->num == 0)
        return true;
    pdata = (PHONE_DATA *)(phone->data + *len);
	S8 strength = getField_Strength();
    I32 oneDayNum = atoi(dev_conf.ftp.ftpSingleFileNum);
    if (*len != 0)
    {
        if (strcmp(dev_conf.dev.protocol, KUANGUANG) == 0)
        {
            for (i = 0; i < num; i++)
            {
                if (strlen((char *)pdata->imei) == 4)
                    updata->len += sprintf(updata->data + updata->len, "%s%s\t%s\t%s\t\t%d\t1\r\n", 
                          dev_conf.ftp.ftpbsic, getCarrierCode(pdata->slotId), pdata->imsi, "", pdata->time_low);
                else
                    updata->len += sprintf(updata->data + updata->len, "%s%s\t%s\t%s\t\t%d\t1\r\n", 
                          dev_conf.ftp.ftpbsic, getCarrierCode(pdata->slotId), pdata->imsi, pdata->imei, pdata->time_low);

                if ( updata->time < pdata->time_low)
                {
                    updata->time = pdata->time_low;
                }
                updata->num++;
                phone->num--;
                pdata++;
                
            }
        }
        else if(strcmp(dev_conf.dev.protocol, FENGHUO) == 0)
		{
			for (i = 0; i < num; i++)
            {
                updata->len += sprintf(updata->data + updata->len, "{\"capture_time\":\"%d\",\"imsi\":\"%s\",\"imei\":\"\",\"phone_seven_number\":\"\",\"phoneattribution\":\"\",\"isp_id\":\"\",\"signal_intensity\":\"%d\",\"device_id\":\"%s%s\",\"upareaid\":\"%s\",\"longitude\":\"%s\",\"latitude\":\"%s\",\"siteid\":\"%s\",\"huntertype\":\"\",\"hunterformat\":\"\"}\n", 
					pdata->time_low, pdata->imsi, strength, dev_conf.ftp.ftpcompany, mac12, dev_conf.ftp.ftpDataSource, dev_conf.dev.pos_lo, 
					dev_conf.dev.pos_la, dev_conf.ftp.ftpbsic);
				if ( updata->time < pdata->time_low)
                {
                    updata->time = pdata->time_low;
                }
                updata->num++;
                phone->num--;
                pdata++;
            }
        }
        else if (strcmp(dev_conf.dev.protocol, XINAN) == 0 || strcmp(dev_conf.dev.protocol, CAIPIN) == 0
            || strcmp(dev_conf.dev.protocol, CAIPINNEW) == 0 )
        {
            char szbuf[64] = {0};
            for (i = 0; i < num; i++)
            {
                memset(szbuf, 0, 64);
        
                strftime(szbuf, 64, "%Y-%m-%d %H:%M:%S", localtime(((time_t *)(&pdata->time_low))));
                if (strlen((char *)pdata->imei) <= 4)
                {
                    updata->len += sprintf(updata->data + updata->len, "%s\t%s\t%s\r\n", pdata->imsi, "00000000000000", szbuf);
                }
                else
                {
                    pdata->imei[14] = '\0';
                    updata->len += sprintf(updata->data + updata->len, "%s\t%s\t%s\r\n", pdata->imsi, pdata->imei, szbuf);
                } 
                if ( updata->time < pdata->time_low)
                {
                    updata->time = pdata->time_low;
                }
                updata->num++;
                phone->num--;
                pdata++;
            }
        }
        else if (strcmp(dev_conf.dev.protocol, HUANSHUO) == 0 )
        {
            char szbuf[64] = {0};
            for (i = 0; i < num; i++)
            {
                memset(szbuf, 0, 64);
        
                strftime(szbuf, 64, "%Y-%m-%d %H:%M:%S", localtime(((time_t *)(&pdata->time_low))));
                if (strlen((char *)pdata->imei) <= 4)
                {
                    updata->len += sprintf(updata->data + updata->len, "%s\t%s\t%s\t%s\t%s\r\n", pdata->imsi, "00000000000000", szbuf, dev_conf.dev.pos_lo, dev_conf.dev.pos_la);   
                }
                else
                {
                    pdata->imei[14] = '\0';
                    updata->len += sprintf(updata->data + updata->len, "%s\t%s\t%s\t%s\t%s\r\n", pdata->imsi, pdata->imei, szbuf, dev_conf.dev.pos_lo, dev_conf.dev.pos_la);
                } 
                if ( updata->time < pdata->time_low)
                {
                    updata->time = pdata->time_low;
                }
                updata->num++;
                phone->num--;
                pdata++;
            }
        }
        else if ( strcmp(dev_conf.dev.protocol, WANGXUN) == 0 )
		{
            char szbuf[64] = {0};
            for (i = 0; i < num; i++)
            {
                memset(szbuf, 0, 64);
        
                strftime(szbuf, 64, "%Y%m%d%H%M%S", localtime(((time_t *)(&pdata->time_low))));
                if (strlen((char *)pdata->imei) == 4)
                {
                    updata->len += sprintf(updata->data + updata->len, "%s%s|%s|%s|%s|%s|%s|\r\n", 
					dev_conf.ftp.ftpbsic, WXgetCarrierCode(pdata->slotId), pdata->imsi, "", "", szbuf, "1");
				}
                else
                {
					updata->len += sprintf(updata->data + updata->len, "%s%s|%s|%s|%s|%s|%s|\r\n", 
					dev_conf.ftp.ftpbsic, WXgetCarrierCode(pdata->slotId), pdata->imsi, pdata->imei, "", szbuf, "1");
                } 
                if ( updata->time < pdata->time_low)
                {
                    updata->time = pdata->time_low;
                }
                updata->num++;
                phone->num--;
                pdata++;
            }
        }
        else if (strcmp(dev_conf.dev.protocol, RENZIXING) == 0)
        {
            updata->len += sprintf(updata->data,"%s", "[");
            for (i = 0; i < num; i++)
            {
                 updata->len += sprintf(updata->data + updata->len,  
                 "{\"BEGIN_TIME\":%d,\"EVENT\":\"27\",\"USER_NUM\":\"\",\"HOME_AREA\":\"\",\"RELATE_NUM\":\"\",\"RELATE_HOME_AC\":\"\",\"IMSI\":\"%s\",\"IMEI\":\"\",\"CUR_AREA\":\"\",\"NE_ID\":\"\",\"LAI\":0,\"CI\":\"\",\"LONGITUDE\":\"%s\",\"LATITUDE\":\"%s\",\"OLDLAI\":0,\"OLDCI\":\"\",\"OLDLONGITUDE\":\"\",\"OLDLATITUDE\":\"\",\"DEVMAC\":\"%s\",\"DEVICENUM\":\"%s%s\",\"SERVICECODE\":\"%s\",\"COMPANY_ID\":\"%s\"}",
                pdata->time_low, pdata->imsi, dev_conf.dev.pos_lo, dev_conf.dev.pos_la, mac17, dev_conf.ftp.ftpcompany,  mac12, dev_conf.ftp.ftpplaceCode, dev_conf.ftp.ftpcompany);
                if ( updata->time < pdata->time_low)
                {
                    updata->time = pdata->time_low;
                }
                updata->num++;
                phone->num--;
                pdata++;
            }        
        }
        else if (strcmp(dev_conf.dev.protocol, GAOKE) == 0)
        {
            updata->len += sprintf(updata->data,"%s", "[");
            char szbuf[64] = {0};
            for (i = 0; i < num; i++)
            {
                memset(szbuf, 0, 64);
        
                strftime(szbuf, 64, "%Y%m%d%H%M%S", localtime(((time_t *)(&pdata->time_low))));
                if (strlen((char *)pdata->imei) == 4)
                {
                    updata->len += sprintf(updata->data + updata->len, "{\"CJSJ\":\"%s\",\"MSISDN\":\"\",\"IMEI\":\"\",\"IMSI\":\"%s\",\"VENDOR\":\"%s\",\"RSSI\":80,\"ORIGINAL_DEVICE_ID\":\"%s\",\"DATA_SRC\":\"%s\",\"ADDRESS\":\"%s\",\"LATITUDE\":%f,\"LONGITUDE\":%f,\"TELECOM_OPERATORS\":\"%s\",\"MOBILE_AREA\":\"\"}", 
					szbuf, pdata->imsi, dev_conf.ftp.ftpcompany, dev_conf.ftp.ftpbsic, dev_conf.ftp.ftpDataSource, dev_conf.dev.devAddr, atof(dev_conf.dev.pos_la), 
                    atof(dev_conf.dev.pos_lo), GKgetOperatorCode(pdata->slotId));
				}
                else
                {
                    updata->len += sprintf(updata->data + updata->len, "{\"CJSJ\":\"%s\",\"MSISDN\":\"\",\"IMEI\":\"%s\",\"IMSI\":\"%s\",\"VENDOR\":\"%s\",\"RSSI\":80,\"ORIGINAL_DEVICE_ID\":\"%s\",\"DATA_SRC\":\"%s\",\"ADDRESS\":\"%s\",\"LATITUDE\":%f,\"LONGITUDE\":%f,\"TELECOM_OPERATORS\":\"%s\",\"MOBILE_AREA\":\"\"}", 
					szbuf, pdata->imei, pdata->imsi, dev_conf.ftp.ftpcompany, dev_conf.ftp.ftpbsic, dev_conf.ftp.ftpDataSource, dev_conf.dev.devAddr, atof(dev_conf.dev.pos_la), 
                    atof(dev_conf.dev.pos_lo), GKgetOperatorCode(pdata->slotId));
                } 
                if ( updata->time < pdata->time_low)
                {
                    updata->time = pdata->time_low;
                }
                updata->num++;
                phone->num--;
                pdata++;
            }        
        }
        else if (strcmp(dev_conf.dev.protocol, JINFENG) == 0)
        {
            char szbuf[64] = {0};
            char day[12] = {0};
            for (i = 0; i < num; i++)
            {
                if (updata->num == atoi(dev_conf.ftp.ftpmaxcount))
                    break;
                memset(szbuf, 0, 64);
                strftime(szbuf, 64, "%Y%m%d%H%M%S", localtime(((time_t *)(&pdata->time_low))));
                memcpy(day, szbuf, 8);
                if (strcmp(dev_conf.ftp.ftpCompressFileNum, day) != 0)
                {
                    strcpy(dev_conf.ftp.ftpCompressFileNum, day);
                    oneDayNum = 0;
                }
                if (strlen((char *)pdata->imei) == 4)
                {
                    updata->len += sprintf(updata->data + updata->len, "%s,%s,,%s,,,,%s,%s,%s,,,,,,%d,%d,,,,,%d,,,,,\n", szbuf, dev_conf.ftp.ftpplaceCode,
                    pdata->imsi, dev_conf.ftp.ftpbsic, dev_conf.dev.pos_lo, dev_conf.dev.pos_la, JFgetOperatorCode(pdata->slotId),
                    JFgetCarrierCode(pdata->slotId),
                    oneDayNum++);                }
                else
                {
                    updata->len += sprintf(updata->data + updata->len, 
                   "%s,%s,,%s,%s,,,%s,%s,%s,,,,,,%d,%d,,,,,%d,,,,,\n", szbuf, dev_conf.ftp.ftpplaceCode,
                    pdata->imsi, pdata->imei, dev_conf.ftp.ftpbsic, dev_conf.dev.pos_lo, dev_conf.dev.pos_la, JFgetOperatorCode(pdata->slotId),
                    JFgetCarrierCode(pdata->slotId),
                    oneDayNum++);
                } 
                if ( updata->time < pdata->time_low)
                {
                    updata->time = pdata->time_low;
                }
                updata->num++;
                phone->num--;
                *len += sizeof(PHONE_DATA);
                pdata++;
                
            }
            memset(dev_conf.ftp.ftpSingleFileNum, 0, DEV_INFO_NUM_LEN);
            sprintf(dev_conf.ftp.ftpSingleFileNum, "%d", oneDayNum);
        }
        else if(strcmp(dev_conf.dev.protocol, ZISHUO) == 0)
		{
            char szbuf[64] = {0};
            for (i = 0; i < num; i++)
            {
                memset(szbuf, 0, 64);
        
                strftime(szbuf, 64, "%Y%m%d%H%M%S", localtime(((time_t *)(&pdata->time_low))));
                if (strlen((char *)pdata->imei) == 4)
                {
                    updata->len += sprintf(updata->data + updata->len, "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%d\t%d\t%d\t\r\n", pdata->imsi, "00000000000000", 
						"0000000", dev_conf.ftp.ftpbsic, szbuf, dev_conf.dev.pos_lo, dev_conf.dev.pos_la, ZSgetOperatorCode(pdata->imsi), 0, ZSgetNetworkCode(pdata->slotId));
				}
                else
                {
                	pdata->imei[14] = '\0';
					updata->len += sprintf(updata->data + updata->len, "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%d\t%d\t%d\t\r\n", pdata->imsi, pdata->imei, 
						"0000000", dev_conf.ftp.ftpbsic, szbuf, dev_conf.dev.pos_lo, dev_conf.dev.pos_la, ZSgetOperatorCode(pdata->imsi), 0, ZSgetNetworkCode(pdata->slotId));
                } 
                if ( updata->time < pdata->time_low)
                {
                    updata->time = pdata->time_low;
                }
                updata->num++;
                phone->num--;
                pdata++;
            }
        }	
    }
    else
    {
        if (fd == NULL)
        {
            if (strcmp(dev_conf.dev.protocol, KUANGUANG) == 0)
            {
/*                
                I8 szbuf[64] = {0};
                time_t uptime;
                time(&uptime);
                strftime(szbuf, 64, "%Y%m%d%H%M%S", localtime(&uptime));
                memset(fileName, 0, DEV_INFO_PAR2_LEN);
                sprintf(fileName, "%s/Hotpoint_0_%s_0_%s.hp", recordDir, dev_conf.ftp.ftpbsic, szbuf);
                fd = fopen(fileName, "a");
                if (fd == NULL)
                {
                    cli_app_log(CLI_LOG_ERROR, "can not open file %s \n", fileName);
                    return false;
                }
*/              
                time_t uptime;
                time(&uptime);
                I32 singleNum = atoi(dev_conf.ftp.ftpSingleFileNum);
                sprintf(kuanGuangFtp.ftpFiles[kuanGuangFtp.fileNum].fileName, "%s/%s-145-%s-%lu-%05d-WA_SOURCE_EW_1001-0.bcp", 
                        recordDir, dev_conf.ftp.ftpbsic, dev_conf.ftp.ftpDataSource, uptime, singleNum++);
                fd = fopen(kuanGuangFtp.ftpFiles[kuanGuangFtp.fileNum++].fileName, "a");
                cli_app_log(CLI_LOG_INFO, "open file name is %s \n", kuanGuangFtp.ftpFiles[kuanGuangFtp.fileNum - 1].fileName);
                if (fd == NULL)
                {
                    cli_app_log(CLI_LOG_ERROR, "can not open file %s \n", kuanGuangFtp.ftpFiles[kuanGuangFtp.fileNum - 1].fileName);
                    return false;                
                }
                memset(dev_conf.ftp.ftpSingleFileNum, 0, DEV_INFO_NUM_LEN);
                if (singleNum > 99999)
                    singleNum = 0;
                sprintf(dev_conf.ftp.ftpSingleFileNum, "%d", singleNum);

            }
			else if(strcmp(dev_conf.dev.protocol, FENGHUO) == 0)
			{
				time_t uptime;
                time(&uptime);
                static I32 singleNum = 0;
				U32	random = rand()%100000;
				
				sprintf(ftpFile, "996_%s_%lu_%05d_DATA-DEVICEGSM_%d", dev_conf.ftp.ftpDataSource, uptime, random, singleNum++);
				sprintf(kuanGuangFtp.ftpFiles[kuanGuangFtp.fileNum].fileName, "%s/%s.bjson", recordDir, ftpFile);
				
				kuanGuangFtp.uploadType = 0;
                fd = fopen(kuanGuangFtp.ftpFiles[kuanGuangFtp.fileNum++].fileName, "a");
                cli_app_log(CLI_LOG_INFO, "open file name is %s \n", kuanGuangFtp.ftpFiles[kuanGuangFtp.fileNum - 1].fileName);
                if (fd == NULL)
                {
                    cli_app_log(CLI_LOG_ERROR, "can not open file %s \n", kuanGuangFtp.ftpFiles[kuanGuangFtp.fileNum - 1].fileName);
                    return false;                
                }	

                memset(dev_conf.ftp.ftpSingleFileNum, 0, DEV_INFO_NUM_LEN);
                if (singleNum > 99999)
                    singleNum = 0;
                sprintf(dev_conf.ftp.ftpSingleFileNum, "%d", singleNum);
				cli_app_log(CLI_LOG_INFO, "open file name is %s   \n", kuanGuangFtp.ftpFiles[kuanGuangFtp.fileNum - 1].fileName);
				cli_app_log(CLI_LOG_INFO, "open file name is %s   \n", ftpFile);
            }
            else if (strcmp(dev_conf.dev.protocol, XINAN) == 0 || strcmp(dev_conf.dev.protocol, CAIPIN) == 0
                || strcmp(dev_conf.dev.protocol, CAIPINNEW) == 0  || strcmp(dev_conf.dev.protocol, HUANSHUO) == 0
                || strcmp(dev_conf.dev.protocol, GAOKE) == 0)
            {
                I8 szbuf[64] = {0};
                time_t uptime;
                time(&uptime);
                strftime(szbuf, 64, "%Y%m%d%H%M%S", localtime(&uptime));
                memset(fileName, 0, DEV_INFO_PAR2_LEN);
                if (strcmp(dev_conf.dev.protocol, XINAN) == 0 )
                {
                    memset(ftpFile, 0, DEV_INFO_PAR2_LEN);
                    if (atoi(dev_conf.capLte.isSupported) == 0)
                    {
                        sprintf(fileName, "%s/%s_%s_%s_GSM.tmp", recordDir, dev_conf.ftp.ftpcompany, dev_conf.ftp.ftpbsic, szbuf);
                        sprintf(ftpFile, "%s_%s_%s_GSM", dev_conf.ftp.ftpcompany, dev_conf.ftp.ftpbsic, szbuf);
                    }
                    else
                    {
                        sprintf(fileName, "%s/%s_%s_%s.tmp", recordDir, dev_conf.ftp.ftpcompany, dev_conf.ftp.ftpbsic, szbuf);
                        sprintf(ftpFile, "%s_%s_%s", dev_conf.ftp.ftpcompany, dev_conf.ftp.ftpbsic, szbuf);

                    }
                }
                else
                {
                    if (atoi(dev_conf.capLte.isSupported) == 0)
                        sprintf(fileName, "%s/%s_%s_%s_GSM.txt", recordDir, dev_conf.ftp.ftpcompany, dev_conf.ftp.ftpbsic, szbuf);
                    else
                        sprintf(fileName, "%s/%s_%s_%s.txt", recordDir, dev_conf.ftp.ftpcompany, dev_conf.ftp.ftpbsic, szbuf);
                }
                fd = fopen(fileName, "a");
                cli_app_log(CLI_LOG_INFO, "open file name is %s \n", fileName);
                if (fd == NULL)
                {
                    cli_app_log(CLI_LOG_ERROR, "can not open file %s \n", fileName);
                    return false;
                }
            }
            else if (strcmp(dev_conf.dev.protocol, WANGXUN) == 0)
            {
                I8 szbuf[64] = {0};
                time_t uptime;
                time(&uptime);
                strftime(szbuf, 64, "%Y%m%d%H%M%S", localtime(&uptime));
                memset(fileName, 0, DEV_INFO_PAR2_LEN);
                memset(ftpFile, 0, DEV_INFO_PAR2_LEN);
				sprintf(fileName, "%s/%s-%s.txt", recordDir, "BYGJ", szbuf);
				sprintf(ftpFile, "%s/%s-%s", recordDir, "BYGJ", szbuf);
                fd = fopen(fileName, "a");
                cli_app_log(CLI_LOG_INFO, "open file name is %s \n", fileName);
                if (fd == NULL)
                {
                    cli_app_log(CLI_LOG_ERROR, "can not open file %s \n", fileName);
                    return false;
                }
            }
            else if (strcmp(dev_conf.dev.protocol, RENZIXING) == 0)
            {
                I8 szbuf[64] = {0};
                time_t uptime;
                time(&uptime);
                strftime(szbuf, 64, "%Y%m%d%H%M%S", localtime(&uptime));
                memset(fileName, 0, DEV_INFO_PAR2_LEN);
                memset(ftpFile, 0, DEV_INFO_PAR2_LEN);
                sprintf(ftpFile, "%s%03d_146_%s_%s_014.log",  szbuf, rand()%1000, dev_conf.ftp.ftpDataSource, 
                            dev_conf.ftp.ftpcompany);

                sprintf(fileName, "%s/%s.tmp", recordDir, ftpFile);                 
                fd = fopen(fileName, "a");
                cli_app_log(CLI_LOG_INFO, "open file name is %s \n", fileName);
                if (fd == NULL)
                {
                    cli_app_log(CLI_LOG_ERROR, "can not open file %s \n", fileName);
                    return false;
                }
                kuanGuangFtp.uploadType = IMSI_DATA_TYPE;
                kuanGuangFtp.fileNum = 1;
            }
            else if (strcmp(dev_conf.dev.protocol, JINFENG) == 0)
            {
                I8 szbuf[64] = {0};
                strftime(szbuf, 64, "%Y%m%d%H%M%S", localtime((time_t *)&pdata->time_low));
                memset(fileName, 0, DEV_INFO_PAR2_LEN);
                memset(ftpFile, 0, DEV_INFO_PAR2_LEN);
				sprintf(fileName, "%s/EVT-%s-%s-%s.dat", recordDir, dev_conf.ftp.ftpplaceCode, dev_conf.ftp.ftpcompany ,szbuf);
				sprintf(ftpFile, "%s/EVT-%s-%s-%s", recordDir, dev_conf.ftp.ftpplaceCode, dev_conf.ftp.ftpcompany, szbuf);
                fd = fopen(fileName, "a");
                cli_app_log(CLI_LOG_INFO, "open file name is %s \n", fileName);
                if (fd == NULL)
                {
                    cli_app_log(CLI_LOG_ERROR, "can not open file %s \n", fileName);
                    return false;
                }
                
            }
            else if(strcmp(dev_conf.dev.protocol, ZISHUO) == 0)
            {
				I8 szbuf[64] = {0};
                time_t uptime;
                time(&uptime);
                strftime(szbuf, 64, "%Y%m%d%H%M%S", localtime(&uptime));
                memset(fileName, 0, DEV_INFO_PAR2_LEN);
                memset(ftpFile, 0, DEV_INFO_PAR2_LEN);
                sprintf(fileName, "%s/%s_%s_%s_%s.txt", recordDir, "imsiimei", "surezs", dev_conf.dev.devNum, szbuf);
                sprintf(ftpFile, "%s/%s_%s_%s_%s", recordDir, "imsiimei", "surezs", dev_conf.dev.devNum, szbuf);
                fd = fopen(fileName, "a");
                cli_app_log(CLI_LOG_INFO, "open file name is %s \n", fileName);
                if (fd == NULL)
                {
                    cli_app_log(CLI_LOG_ERROR, "can not open file %s \n", fileName);
                    return false;
                }

            }
        }

        if (updata->len != 0)
        {
            cli_app_log(CLI_LOG_INFO, "save in buf num is %d len is %d \n", updata->num, updata->len);
            fwrite(updata->data, updata->len, 1, fd);
            kuanGuangFtp.ftpFiles[kuanGuangFtp.fileNum - 1].fileRows = updata->num;
            updata->len = 0;
        }
        
        if (strcmp(dev_conf.dev.protocol, KUANGUANG) == 0)
        {
            for (i = 0; i < num; i++)
            {
                if (updata->num == atoi(dev_conf.ftp.ftpmaxcount))
                {
                    fclose(fd);
                    fd = NULL;
                    cli_app_log(CLI_LOG_INFO, "move record to others i is %d  num is %d  max count %s\n", i, num, dev_conf.ftp.ftpmaxcount);
                    if (kuanGuangFtp.fileNum == MAX_FTP_FILES)
                        break;
                    time_t uptime;
                    time(&uptime);
                    I32 singleNum = atoi(dev_conf.ftp.ftpSingleFileNum);
                    sprintf(kuanGuangFtp.ftpFiles[kuanGuangFtp.fileNum].fileName, "%s/%s-145-%s-%lu-%05d-WA_SOURCE_EW_1001-0.bcp", 
                            recordDir, dev_conf.ftp.ftpbsic, dev_conf.ftp.ftpDataSource, uptime, singleNum++);
                    fd = fopen(kuanGuangFtp.ftpFiles[kuanGuangFtp.fileNum++].fileName, "a");
                    cli_app_log(CLI_LOG_INFO, "open file is %s \n", kuanGuangFtp.ftpFiles[kuanGuangFtp.fileNum - 1].fileName);
                    if (fd == NULL)
                    {
                        cli_app_log(CLI_LOG_ERROR, "can not open file %s \n", kuanGuangFtp.ftpFiles[kuanGuangFtp.fileNum - 1].fileName);
                        return false;                
                    }
                    memset(dev_conf.ftp.ftpSingleFileNum, 0, DEV_INFO_NUM_LEN);
                    if (singleNum > 99999)
                        singleNum = 0;
                    sprintf(dev_conf.ftp.ftpSingleFileNum, "%d", singleNum);
                    memset(updata->data, 0 , updata->len);
                    updata->num = 0;
                }
                if (strlen((char *)pdata->imei) == 4)
                    fprintf(fd, "%s%s\t%s\t%s\t\t%d\t1\r\n", 
                        dev_conf.ftp.ftpbsic, getCarrierCode(pdata->slotId), pdata->imsi, "", pdata->time_low);
                else
                    fprintf(fd, "%s%s\t%s\t%s\t\t%d\t1\r\n", 
                        dev_conf.ftp.ftpbsic, getCarrierCode(pdata->slotId), pdata->imsi, pdata->imei, pdata->time_low);
                if ( updata->time < pdata->time_low)
                {
                    updata->time = pdata->time_low;
                }
                updata->num++;
                phone->num--;
                *len += sizeof(PHONE_DATA);
                pdata++;
                kuanGuangFtp.ftpFiles[kuanGuangFtp.fileNum - 1].fileRows++;
            }
        }
        else if (strcmp(dev_conf.dev.protocol, FENGHUO) == 0)
        {
			for (i = 0; i < num; i++)
            {
                if (updata->num == atoi(dev_conf.ftp.ftpmaxcount))
                {
                    fclose(fd);
                    fd = NULL;
                    /*
                    cli_app_log(CLI_LOG_INFO, "move record to others i is %d  num is %d  max count %s\n", i, num, dev_conf.ftp.ftpmaxcount);
                    if (kuanGuangFtp.fileNum == MAX_FTP_FILES)
                        break;
                    time_t uptime;
                    time(&uptime);
                    I32 singleNum = atoi(dev_conf.ftp.ftpSingleFileNum);
                    sprintf(kuanGuangFtp.ftpFiles[kuanGuangFtp.fileNum].fileName, "%s/145-%s-%lu-%05d-WA_SOURCE_FJ_0001-0.bcp", 
                        recordDir, dev_conf.ftp.ftpDataSource, uptime, singleNum++);
                    fd = fopen(kuanGuangFtp.ftpFiles[kuanGuangFtp.fileNum++].fileName, "a");
                    cli_app_log(CLI_LOG_INFO, "open file is %s \n", kuanGuangFtp.ftpFiles[kuanGuangFtp.fileNum - 1].fileName);
                    if (fd == NULL)
                    {
                        cli_app_log(CLI_LOG_ERROR, "can not open file %s \n", kuanGuangFtp.ftpFiles[kuanGuangFtp.fileNum - 1].fileName);
                        return false;                
                    }
                    memset(dev_conf.ftp.ftpSingleFileNum, 0, DEV_INFO_NUM_LEN);
                    if (singleNum > 99999)
                        singleNum = 0;
                    sprintf(dev_conf.ftp.ftpSingleFileNum, "%d", singleNum);
                    memset(updata->data, 0 , updata->len);
                    updata->num = 0;*/
                    break;
                }
				
				fprintf(fd, "{\"capture_time\":\"%d\",\"imsi\":\"%s\",\"imei\":\"\",\"phone_seven_number\":\"\",\"phoneattribution\":\"\",\"isp_id\":\"\",\"signal_intensity\":\"%d\",\"device_id\":\"%s%s\",\"upareaid\":\"%s\",\"longitude\":\"%s\",\"latitude\":\"%s\",\"siteid\":\"%s\",\"huntertype\":\"\",\"hunterformat\":\"\"}\n", 
					pdata->time_low, pdata->imsi, strength, dev_conf.ftp.ftpcompany, mac12, dev_conf.ftp.ftpDataSource, dev_conf.dev.pos_lo, 
					dev_conf.dev.pos_la, dev_conf.ftp.ftpbsic);                
                if ( updata->time < pdata->time_low)
                {
                    updata->time = pdata->time_low;
                }
                updata->num++;
                phone->num--;
                *len += sizeof(PHONE_DATA);
                pdata++;
                kuanGuangFtp.ftpFiles[kuanGuangFtp.fileNum - 1].fileRows++;
            }
			
        }
        else if (strcmp(dev_conf.dev.protocol, XINAN) == 0 || strcmp(dev_conf.dev.protocol, CAIPIN) == 0
            || strcmp(dev_conf.dev.protocol, CAIPINNEW) == 0 )
        {
            char szbuf[64] = {0};
            for (i = 0; i < num; i++)
            {
                if (updata->num == atoi(dev_conf.ftp.ftpmaxcount))
                    break;
                memset(szbuf, 0, 64);
                strftime(szbuf, 64, "%Y-%m-%d %H:%M:%S", localtime(((time_t *)(&pdata->time_low))));
                if (strlen((char *)pdata->imei) == 4)
                {
                    fprintf(fd, "%s\t%s\t%s\r\n", pdata->imsi, "00000000000000", szbuf);
                }
                else
                {
                    pdata->imei[14] = '\0';
                    cli_app_log(CLI_LOG_INFO, "STRLEN is %d \n", strlen((char *)pdata->imei) );
                    fprintf(fd, "%s\t%s\t%s\r\n", pdata->imsi, pdata->imei, szbuf);
                } 
                if ( updata->time < pdata->time_low)
                {
                    updata->time = pdata->time_low;
                }
                updata->num++;
                phone->num--;
                *len += sizeof(PHONE_DATA);
                pdata++;
                
            }
        }
        else if (strcmp(dev_conf.dev.protocol, JINFENG) == 0)
        {
            char szbuf[64] = {0};
            char day[12] = {0};
            for (i = 0; i < num; i++)
            {
                if (updata->num == atoi(dev_conf.ftp.ftpmaxcount))
                    break;
                memset(szbuf, 0, 64);
                strftime(szbuf, 64, "%Y%m%d%H%M%S", localtime(((time_t *)(&pdata->time_low))));
                memcpy(day, szbuf, 8);
                if (strcmp(dev_conf.ftp.ftpCompressFileNum, day) != 0)
                {
                    strcpy(dev_conf.ftp.ftpCompressFileNum, day);
                    oneDayNum = 0;
                }
                if (strlen((char *)pdata->imei) == 4)
                {
                    fprintf(fd, "%s,%s,,%s,,,,%s,%s,%s,,,,,,%d,%d,,,,,%d,,,,,\n", szbuf, dev_conf.ftp.ftpplaceCode,
                    pdata->imsi, dev_conf.ftp.ftpbsic, dev_conf.dev.pos_lo, dev_conf.dev.pos_la, JFgetOperatorCode(pdata->slotId),
                    JFgetCarrierCode(pdata->slotId),
                    oneDayNum++);
                }
                else
                {
                    fprintf(fd, "%s,%s,,%s,%s,,,%s,%s,%s,,,,,,%d,%d,,,,,%d,,,,,\n", szbuf, dev_conf.ftp.ftpplaceCode,
 pdata->imsi, pdata->imei, dev_conf.ftp.ftpbsic, dev_conf.dev.pos_lo, dev_conf.dev.pos_la, JFgetOperatorCode(pdata->slotId), JFgetCarrierCode(pdata->slotId),
                    oneDayNum++);
                } 
                if ( updata->time < pdata->time_low)
                {
                    updata->time = pdata->time_low;
                }
                updata->num++;
                phone->num--;
                *len += sizeof(PHONE_DATA);
                pdata++;
                
            }
            memset(dev_conf.ftp.ftpSingleFileNum, 0, DEV_INFO_NUM_LEN);
            sprintf(dev_conf.ftp.ftpSingleFileNum, "%d", oneDayNum);
        }
        else if(strcmp(dev_conf.dev.protocol, HUANSHUO) == 0)
        {
            char szbuf[64] = {0};
            for (i = 0; i < num; i++)
            {
                if (updata->num == atoi(dev_conf.ftp.ftpmaxcount))
                    break;
                memset(szbuf, 0, 64);
                strftime(szbuf, 64, "%Y-%m-%d %H:%M:%S", localtime(((time_t *)(&pdata->time_low))));
                if (strlen((char *)pdata->imei) == 4)
                {
                    fprintf(fd, "%s\t%s\t%s\t%s\t%s\r\n", pdata->imsi, "00000000000000", szbuf, dev_conf.dev.pos_lo, dev_conf.dev.pos_la);                
                }
                else
                {
                    pdata->imei[14] = '\0';
                    cli_app_log(CLI_LOG_INFO, "STRLEN is %d \n", strlen((char *)pdata->imei) );
                    fprintf(fd, "%s\t%s\t%s\t%s\t%s\r\n", pdata->imsi, pdata->imei, szbuf, dev_conf.dev.pos_lo, dev_conf.dev.pos_la);                
                } 
                if ( updata->time < pdata->time_low)
                {
                    updata->time = pdata->time_low;
                }
                updata->num++;
                phone->num--;
                *len += sizeof(PHONE_DATA);
                pdata++;
                
            }
        }
		else if (strcmp(dev_conf.dev.protocol, WANGXUN) == 0)
        {
            char szbuf[64] = {0};
            for (i = 0; i < num; i++)
            {
                if (updata->num == atoi(dev_conf.ftp.ftpmaxcount))
                    break;
                memset(szbuf, 0, 64);
                strftime(szbuf, 64, "%Y%m%d%H%M%S", localtime(((time_t *)(&pdata->time_low))));
                if (strlen((char *)pdata->imei) == 4)
                {
					fprintf(fd, "%s%s|%s|%s|%s|%s|%s|\r\n", dev_conf.ftp.ftpbsic, WXgetCarrierCode(pdata->slotId), pdata->imsi, "", "", szbuf, "1");
                }
                else
                {
					cli_app_log(CLI_LOG_INFO, "STRLEN is %d \n", strlen((char *)pdata->imei) );
					fprintf(fd, "%s%s|%s|%s|%s|%s|%s|\r\n", dev_conf.ftp.ftpbsic, WXgetCarrierCode(pdata->slotId), pdata->imsi, pdata->imei, "", szbuf, "1");
                } 
                if ( updata->time < pdata->time_low)
                {
                    updata->time = pdata->time_low;
                }
                updata->num++;
                phone->num--;
                *len += sizeof(PHONE_DATA);
                pdata++;
                
            }
        }
        else if (strcmp(dev_conf.dev.protocol, RENZIXING) == 0)
        {
            for (i = 0; i < num; i++)
            {
                if (updata->num == atoi(dev_conf.ftp.ftpmaxcount))
                {
                    break;
                }

                if (updata->num == 0)
                {
                    fprintf(fd, "%s", "[");
                }
                else
                {   
                    fprintf(fd, "%s", ",");
                }
                fprintf(fd, "{\"BEGIN_TIME\":%d,\"EVENT\":\"27\",\"USER_NUM\":\"\",\"HOME_AREA\":\"\",\"RELATE_NUM\":\"\",\"RELATE_HOME_AC\":\"\",\"IMSI\":\"%s\",\"IMEI\":\"\",\"CUR_AREA\":\"\",\"NE_ID\":\"\",\"LAI\":0,\"CI\":\"\",\"LONGITUDE\":\"%s\",\"LATITUDE\":\"%s\",\"OLDLAI\":0,\"OLDCI\":\"\",\"OLDLONGITUDE\":\"\",\"OLDLATITUDE\":\"\",\"DEVMAC\":\"%s\",\"DEVICENUM\":\"%s%s\",\"SERVICECODE\":\"%s\",\"COMPANY_ID\":\"%s\"}",
                pdata->time_low, pdata->imsi, dev_conf.dev.pos_lo, dev_conf.dev.pos_la, mac17, dev_conf.ftp.ftpcompany,  mac12, dev_conf.ftp.ftpplaceCode,dev_conf.ftp.ftpcompany);

                if ( updata->time < pdata->time_low)
                {
                    updata->time = pdata->time_low;
                }
                updata->num++;
                phone->num--;
                *len += sizeof(PHONE_DATA);
                pdata++;
                
            }
            
        }
        else if (strcmp(dev_conf.dev.protocol, GAOKE) == 0)
        {
            char szbuf[64] = {0};

            for (i = 0; i < num; i++)
            {
                if (updata->num == atoi(dev_conf.ftp.ftpmaxcount))
                {
                    break;
                }

                if (updata->num == 0)
                {
                    fprintf(fd, "%s", "[");
                }
                else
                {   
                    fprintf(fd, "%s", ",");
                }
                memset(szbuf, 0, 64);
        
                strftime(szbuf, 64, "%Y%m%d%H%M%S", localtime(((time_t *)(&pdata->time_low))));
                if (strlen((char *)pdata->imei) == 4)
                {
                    fprintf(fd, "{\"CJSJ\":\"%s\",\"MSISDN\":\"\",\"IMEI\":\"\",\"IMSI\":\"%s\",\"VENDOR\":\"%s\",\"RSSI\":80,\"ORIGINAL_DEVICE_ID\":\"%s\",\"DATA_SRC\":\"%s\",\"ADDRESS\":\"%s\",\"LATITUDE\":%f,\"LONGITUDE\":%f,\"TELECOM_OPERATORS\":\"%s\",\"MOBILE_AREA\":\"\"}", 
					szbuf, pdata->imsi, dev_conf.ftp.ftpcompany, dev_conf.ftp.ftpbsic, dev_conf.ftp.ftpDataSource, dev_conf.dev.devAddr, atof(dev_conf.dev.pos_la), 
                    atof(dev_conf.dev.pos_lo), GKgetOperatorCode(pdata->slotId));
				}
                else
                {
                    fprintf(fd, "{\"CJSJ\":\"%s\",\"MSISDN\":\"\",\"IMEI\":\"%s\",\"IMSI\":\"%s\",\"VENDOR\":\"%s\",\"RSSI\":80,\"ORIGINAL_DEVICE_ID\":\"%s\",\"DATA_SRC\":\"%s\",\"ADDRESS\":\"%s\",\"LATITUDE\":%f,\"LONGITUDE\":%f,\"TELECOM_OPERATORS\":\"%s\",\"MOBILE_AREA\":\"\"}", 
					szbuf, pdata->imei, pdata->imsi, dev_conf.ftp.ftpcompany, dev_conf.ftp.ftpbsic, dev_conf.ftp.ftpDataSource, dev_conf.dev.devAddr, atof(dev_conf.dev.pos_la), 
                    atof(dev_conf.dev.pos_lo), GKgetOperatorCode(pdata->slotId));
                } 

                if ( updata->time < pdata->time_low)
                {
                    updata->time = pdata->time_low;
                }
                updata->num++;
                phone->num--;
                *len += sizeof(PHONE_DATA);
                pdata++;
                
            }

        }
        else if(strcmp(dev_conf.dev.protocol, ZISHUO) == 0)
		{
			char szbuf[64] = {0};
			for (i = 0; i < num; i++)
			{
		        if (updata->num == atoi(dev_conf.ftp.ftpmaxcount))
                	break;
				memset(szbuf, 0, 64);
				strftime(szbuf, 64, "%Y%m%d%H%M%S", localtime(((time_t *)(&pdata->time_low))));
				if (strlen((char *)pdata->imei) == 4)
				{
					fprintf(fd, "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%d\t%d\t%d\t\r\n", pdata->imsi, "00000000000000", 
						"0000000", dev_conf.ftp.ftpbsic, szbuf, dev_conf.dev.pos_lo, dev_conf.dev.pos_la, ZSgetOperatorCode(pdata->imsi), 0, ZSgetNetworkCode(pdata->slotId));
				}
				else
				{
					pdata->imei[14] = '\0';
					fprintf(fd, "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%d\t%d\t%d\t\r\n", pdata->imsi, pdata->imei, 
						"0000000", dev_conf.ftp.ftpbsic, szbuf, dev_conf.dev.pos_lo, dev_conf.dev.pos_la, ZSgetOperatorCode(pdata->imsi), 0, ZSgetNetworkCode(pdata->slotId));
				} 
				if ( updata->time < pdata->time_low)
				{
					updata->time = pdata->time_low;
				}
                updata->num++;
                phone->num--;
                *len += sizeof(PHONE_DATA);
                pdata++;
			}
		}
        
        fflush(fd);

    }
    return true;
}

bool fileIsFull()
{
    if (strcmp(dev_conf.dev.protocol, KUANGUANG) == 0)
    {
        if (kuanGuangFtp.fileNum == MAX_FTP_FILES)
            return true;
        else
            return false;
    }
    return true;
}

bool createTerminalDevInfo()
{
    time_t uptime;
    time(&uptime);
    static I32 singleNum = 0;
	static I32 radius = 500;
    sprintf(kuanGuangFtp.ftpFiles[0].fileName, "%s/146-%s-%lu-%05d-WA_BASIC_FJ_1002-0.bcp", recordDir, dev_conf.ftp.ftpDataSource, uptime, singleNum++);
    cli_app_log(CLI_LOG_INFO, "open file is %s \n", kuanGuangFtp.ftpFiles[0].fileName);
    FILE *fdDev = NULL;
    fdDev = fopen(kuanGuangFtp.ftpFiles[0].fileName, "a");
    if (fdDev == NULL)
    {
        cli_app_log(CLI_LOG_WARNING, "open file %s failed \n", kuanGuangFtp.ftpFiles[0].fileName);
        return false;   
    }
    kuanGuangFtp.uploadType = 4;
    kuanGuangFtp.fileNum = 1;
	fprintf(fdDev, "%s\t%s%s\t%s\t%s\t1\t%s\t%s\t%s\t%d\t%d\t\t\t\t", dev_conf.ftp.ftpbsic, dev_conf.ftp.ftpcompany, mac12, dev_conf.dev.devName, 
			dev_conf.ftp.ftpplaceName, dev_conf.ftp.ftpcompany, dev_conf.dev.pos_lo, dev_conf.dev.pos_la, atoi(dev_conf.ftp.ftptimer), radius);
    fclose(fdDev);
    return true;	
}

bool createCompanyBaseInfo()
{
    time_t uptime;
    time(&uptime);
    static I32 singleNum = 0;
    sprintf(kuanGuangFtp.ftpFiles[0].fileName, "%s/146-%s-%lu-%05d-WA_BASIC_FJ_1004-0.bcp", recordDir, dev_conf.ftp.ftpDataSource, uptime, singleNum++);    
    cli_app_log(CLI_LOG_INFO, "open file is %s \n", kuanGuangFtp.ftpFiles[0].fileName);
    FILE *fdBase = NULL;
    fdBase = fopen(kuanGuangFtp.ftpFiles[0].fileName, "a");
    if (fdBase == NULL)
    {
        cli_app_log(CLI_LOG_WARNING, "open file %s failed \n", kuanGuangFtp.ftpFiles[0].fileName);
        return false;   
    }
    kuanGuangFtp.uploadType = 2;
    kuanGuangFtp.fileNum = 1;
	fprintf(fdBase, "%s\t%s\t%s\t%s\t%s\t%s", dev_conf.ftp.ftpcompanyName, dev_conf.ftp.ftpcompany, dev_conf.ftp.ftpcompanyAddr, 
			dev_conf.ftp.ftpcontactor, dev_conf.ftp.ftpcontactorTel, dev_conf.ftp.ftpcontactorMail);
    fclose(fdBase);
    return true;
}

bool createPlaceStateInfo()
{
    time_t uptime;
    time(&uptime);
    static I32 singleNum = 0;
    sprintf(kuanGuangFtp.ftpFiles[0].fileName, "%s/146-%s-%lu-%05d-WA_BASIC_FJ_1003-0.bcp", recordDir, dev_conf.ftp.ftpDataSource, uptime, singleNum++);    
    cli_app_log(CLI_LOG_INFO, "open file is %s \n", kuanGuangFtp.ftpFiles[0].fileName);
    FILE *fdInfo = NULL;
    fdInfo = fopen(kuanGuangFtp.ftpFiles[0].fileName, "a");
    if (fdInfo == NULL)
    {
        cli_app_log(CLI_LOG_WARNING, "open file %s failed \n", kuanGuangFtp.ftpFiles[0].fileName);
        return false;   
    }
    kuanGuangFtp.uploadType = 1;
    kuanGuangFtp.fileNum = 1;

	fprintf(fdInfo, "%s\t%s\t%s\t%s\t%s\t%c\t%c\t\t\t\t\t\t\t%s", 
			dev_conf.ftp.ftpbsic, dev_conf.ftp.ftpplaceName, dev_conf.dev.devAddr, dev_conf.dev.pos_lo, dev_conf.dev.pos_la, '9', '1', dev_conf.ftp.ftpcompany);    
    fclose(fdInfo);
    return true;

}

bool createDeviceJsonFile(char *buf)
{
    time_t uptime;
    time(&uptime);
    static I32 singleNum = 0;
    memset(fileName, 0, DEV_INFO_PAR2_LEN);
    memset(ftpFile, 0, DEV_INFO_PAR2_LEN);
    sprintf(ftpFile, "146-%s-%lu-%05d-DEVICESTATUS-0", dev_conf.ftp.ftpDataSource, uptime, singleNum);
    sprintf(fileName, "%s/146-%s-%lu-%05d-DEVICESTATUS-0.bjson", recordDir, dev_conf.ftp.ftpDataSource, uptime, singleNum++);
    FILE *fdDev = NULL;
    fdDev = fopen(fileName, "a");
    if (fdDev == NULL)
    {
        cli_app_log(CLI_LOG_WARNING, "can not create %s \n", fileName);
        return false;
    }
    kuanGuangFtp.uploadType = 3;
    fprintf(fdDev, "%s", buf);
    fclose(fdDev);
    return true;
}

bool createPlaceJsonFile(char *buf)
{
    time_t uptime;
    time(&uptime);
    static I32 singleNum = 0;
    memset(fileName, 0, DEV_INFO_PAR2_LEN);
    memset(ftpFile, 0, DEV_INFO_PAR2_LEN);
    sprintf(ftpFile, "146-%s-%lu-%05d-SITESTATUS-0", dev_conf.ftp.ftpDataSource, uptime, singleNum);
    sprintf(fileName, "%s/146-%s-%lu-%05d-SITESTATUS-0.bjson", recordDir, dev_conf.ftp.ftpDataSource, uptime, singleNum++);
    FILE *fdPlace = NULL;
    fdPlace = fopen(fileName, "a");
    if (fdPlace == NULL)
    {
        cli_app_log(CLI_LOG_WARNING, "can not create %s \n", fileName);
        return false;
    }
    kuanGuangFtp.uploadType = 3;
    fprintf(fdPlace, "%s", buf);
    fclose(fdPlace);
    return true;

}

const char *getMacFileName()
{
    I8 szbuf[64] = {0};
    time_t uptime;
    time(&uptime);
    static I32 singleNum = 0;

    if (strcmp(dev_conf.dev.protocol, FENGHUO) != 0)
    {
        strftime(szbuf, 64, "%Y%m%d%H%M%S", localtime(&uptime));
        memset(fileName, 0, DEV_INFO_PAR2_LEN);
        sprintf(fileName, "%s/%s_%s_%s_MAC.txt", recordDir, dev_conf.ftp.ftpcompany, dev_conf.ftp.ftpbsic, szbuf);
        return fileName;
    }
    else
    {
    
        sprintf(kuanGuangFtp.ftpFiles[0].fileName, "%s/146-%s-%lu-%05d-WA_SOURCE_FJ_1002-0.bcp", recordDir, dev_conf.ftp.ftpDataSource, uptime, singleNum++);
        cli_app_log(CLI_LOG_INFO, "open file is %s \n", kuanGuangFtp.ftpFiles[0].fileName);

        kuanGuangFtp.uploadType = 5;
        kuanGuangFtp.fileNum = 1;
        return kuanGuangFtp.ftpFiles[0].fileName;
    }
}

I32 *getcurrentFileRows()
{
    return &kuanGuangFtp.ftpFiles[0].fileRows;
}

void resetFileName(void)
{
     memset(&kuanGuangFtp, 0x0, sizeof(KAUNGAUNG_FTP));
}

bool createFtpFile(U8 type)
{
    if (strcmp(dev_conf.dev.protocol, RENZIXING) == 0)
    {
        I8 szbuf[64] = {0};
        time_t uptime;
        time(&uptime);
        strftime(szbuf, 64, "%Y%m%d%H%M%S", localtime(&uptime));
        kuanGuangFtp.uploadType = type;
        I8 province[8] = "000000";
        memcpy(province, dev_conf.ftp.ftpDataSource, 2);
        I8 city[8] = "000000";
        memcpy(city, dev_conf.ftp.ftpDataSource, 4);
        



        switch(type)
        {
            case IMSI_DATA_TYPE:
                {
                    memset(fileName, 0, DEV_INFO_PAR2_LEN);
                    sprintf(fileName, "%s%03d_146_%s_%s_014.log", szbuf, rand()%1000, dev_conf.ftp.ftpDataSource, 
                            dev_conf.ftp.ftpcompany);
                }
                break;

            case PLACE_STATE_TYPE:
                {
                    memset(fileName, 0, DEV_INFO_PAR2_LEN);
                    memset(ftpFile, 0, DEV_INFO_PAR2_LEN);
                    sprintf(ftpFile, "%s%03d_146_%s_%s_008.log", szbuf, rand()%1000, dev_conf.ftp.ftpDataSource, 
                            dev_conf.ftp.ftpcompany);

                    sprintf(fileName, "%s/%s.tmp", recordDir, ftpFile);
                    FILE *placeFd = NULL;
                    placeFd = fopen(fileName, "a");
                    if (placeFd == NULL)
                    {
                        cli_app_log(CLI_LOG_ERROR, "can not create file %s \n", fileName);
                        return false;
                    }
                    fprintf(placeFd, "[{\"SERVICE_CODE\":%s,\"SERVICE_NAME\":%s,\"ADDRESS\":\"%s\",\"ZIP\":\"\",\"BUSINESS_NATURE\":\"8\",\"PRINCIPAL\":\"\", \"PRINCIPAL_TEL\":\"\",\"INFOR_MAN\":\"\",\"INFOR_MAN_TEL\":\"\",\"INFO_MAN_EMAIL\":\"\",\"PRODUCER_CODE\":\"\",\"STATUS\":\"1\",\"ENDING_NUMBER\":\"\",\"SERVER_NUMBER\":\"\",\"EXIT_IP\":\"\",\"AUTH_ACCOUNT\":\"\",\"NET_TYPE\":\"\",\"PRACTITIONER_NUMBER\":\"\", \"NET_MONITOR_DEPARTMENT\":\"\",\"NET_MONITOR_MAN\":\"\",\"NET_MONITOR_MAN_TEL\":\"\",\"REMARK\":\"\",\"SERVICE_TYPE\":\"9\",\"PROVINCE_CODE\":\"%s\",\"CITY_CODE\":\"%s\",\"AREA_CODE\":\"%s\",\"CITY_TYPE\":\"\",\"POLICE_CODE\":\"\",\"MAIL_ACCOUNT\":\"\",\"MOBILE_ACCOUNT\":\"\",\"XPOINT\":\"%s\",\"YPOINT\":\"%s\",\"GIS_XPOINT\":\"\",\"GIS_YPOINT\":\"\",\"TERMINAL_FACTORY_ORGCODE\":\"\",\"ORG_CODE\":\"\",\"IP_TYPE\":\"\",\"BAND_WIDTH\":\"5\",\"NET_LAN\":\"\",\"NET_LAN_TERMINAL\":\"\",\"IS_SAFE\":\"\",\"WIFI_TERMINAL\":\"\",\"PRINCIPAL_CERT_TYPE\":\"\",\"PRINCIPAL_CERT_CODE\":\"\",\"PERSON_NAME\":\"\",\"PERSON_TEL\":\"\",\"PERSON_QQ\":\"\",\"INFOR_MAN_QQ\":\"\",\"START_TIME\":\"\",\"END_TIME\":\"\",\"CREATE_TIME\":\"%s\",\"CAP_TYPE\":\"5\",\"COORDINATE_SYSTEM \":\"03\"}]", dev_conf.ftp.ftpplaceCode,dev_conf.ftp.ftpplaceName,dev_conf.dev.devAddr, province, city, dev_conf.ftp.ftpDataSource, dev_conf.dev.pos_lo,dev_conf.dev.pos_la, dev_conf.dev.createTime);
                    fclose(placeFd);
                    kuanGuangFtp.uploadType = PLACE_STATE_TYPE;
                    return true;

                }
                break;

            case DEV_STATE_TYPE:
                {
                    memset(fileName, 0, DEV_INFO_PAR2_LEN);
                    memset(ftpFile, 0, DEV_INFO_PAR2_LEN);
                    sprintf(ftpFile, "%s%03d_146_%s_%s_009.log", szbuf, rand()%1000, dev_conf.ftp.ftpDataSource, 
                             dev_conf.ftp.ftpcompany);
                    sprintf(fileName, "%s/%s.tmp", recordDir, ftpFile);
                    FILE *devFd = NULL;
                    devFd = fopen(fileName, "a");
                    if (devFd == NULL)
                    {
                        cli_app_log(CLI_LOG_ERROR, "can not create file %s \n", fileName);
                        return false;
                    }
                    fprintf(devFd, "[{\"EQUIPMENT_NUM\":\"%s%s\",\"EQUIPMENT_NAME\":\"\",\"MAC\":\"%s\",\"IP\":\"\",\"SECURITY_FACTORY_ORGCODE\":\"%s\",\"VENDOR_NAME\":\"\",\"VENDOR_NUM\":\"\",\"SERVICE_CODE\":\"%s\",\"PROVINCE_CODE\":\"%s\",\"CITY_CODE\":\"%s\",\"AREA_CODE\":\"%s\",\"INSTALL_DATE\":\"\",\"INSTALL_POINT\":\"\",\"EQUIPMENT_TYPE\":\"91\",\"LONGITUDE\":\"%s\",\"LATITUDE\":\"%s\",\"SUBWAY_STATION\":\"\",\"SUBWAY_LINE_INFO\":\"\",\"SUBWAY_VEHICLE_INFO\":\"\",\"SUBWAY_COMPARTMENT_NUM\":\"\",\"CAR_COD\":\"\",\"UPLOAD_TIME_INTERVAL\":\"%s\",\"COLLECTION_RADIUS\":\"\",\"CREATE_TIME\":\"%s\",\"CREATER\":\"\",\"LAST_CONNECT_TIME\":\"\",\"REMARK\":\"\",\"WDA_VERSION\":\"1.1\",\"FIRMWARE_VERSION\":\"1.1\",\"COORDINATE_SYSTEM\":\"03\"}]", dev_conf.ftp.ftpcompany, mac12, mac17, dev_conf.ftp.ftpcompany, dev_conf.ftp.ftpplaceCode, province, city, dev_conf.ftp.ftpDataSource, dev_conf.dev.pos_lo, dev_conf.dev.pos_la, dev_conf.ftp.ftptimer, dev_conf.dev.createTime);
                    fclose(devFd);
                    kuanGuangFtp.uploadType  = DEV_STATE_TYPE;
                    return true;
                }
                break;
        }

    }
    return false;
}

I8 *getDeviceMac12()
{
    return mac12;
}

bool createFtpDir(U8 type)
{
    if (strcmp(dev_conf.dev.protocol, RENZIXING) == 0)
    {
 
        I8 name[DEV_INFO_NUM_LEN] = {0};

        if (type == DEV_STATE_TYPE)
        {
            strcpy(name, "SBZL");
        }
        else if (type == PLACE_STATE_TYPE)
        {

            strcpy(name, "CSZL");
        }
        else
        {
            strcpy(name, "SJTZ");
        }

        
        if (creatRenzixingFtpDir(dev_conf.ftp.ftpserver, atoi(dev_conf.ftp.ftpport), dev_conf.ftp.ftpuser, 
                        dev_conf.ftp.ftppasswd, name, &ftpDir) < FTP_SUCCESS)
        {
            return false;
        }    
        else
        {
            return true;
        }
    }
    return false;
}

S8 getField_Strength()
{
	S8 ret = 0;
	ret = rand()%31 + 60;
	return -ret;
}
